/**2014-4-26 吕春城
 * 投票
 */

function loading($msg){
    if($msg){
        $('#loading').find('loading').text($msg);
    }
    $('#loading').show();
}
function done(){
    $('#loading').hide();
}

var vote = {
    class_id:'',
    AjaxUrl: '',
    Item_IDS:'',
//页面加载完成要ajax回发获取各项平均值  点击不同菜单项时都要回发获取 再赋值
    GetItemAverage: function (obj) {//获取各项的平均分
        var index = $(obj);
        var item_id = index.val();
        vote.AjaxGetAverage(item_id);
       
    },
    //获取平均分数
    AjaxGetAverage: function (itemid)
    {
        $("#hiddeninput").val(itemid);

        $.get(vote.AjaxUrl, { "itemid": itemid }, function (data) {

            var total = data.total;
            var count=data.count;
            var data=data.data;
            for (var i = 0; i < data.length; i++)
            {
                $('#' + data[i].index).html('平均分'+data[i].average+'&nbsp;&nbsp;计'+data[i].num+'次');
            }
            $('#thisavg').html('平均分为'+total+'&nbsp;共'+count+'次');
        });
        //如果有子分组项应该显示全部值隐藏之外的项
       // var trChoose = $('#data_table .a' + itemid);
        $('#data_table').find('tr').each(function(){
            var theclass='a'+itemid;
            var myclass = $(this).attr("class");
            if (myclass == "a0")
            {
            }
            else if (myclass .search(theclass)>-1) {
                $(this).show();
            }
            else {
                $(this).hide();
            }
        });
    },
    //点击分组事件
    clickGroup:function(obj){//分组选择隐藏不需要显示的部分
        var item_ids = $(obj).val();
        vote.clickGroup2(item_ids);
        //手动点击时还要考虑到子项评价的改变

        var itemid = item_ids.split(',')[0];
        vote.AjaxGetAverage(itemid);
    },
    clickGroup2: function (item_ids) {
        $("#select_Com").find(".table_menu").each(function () {//获取所有item项
            var finddiv = $(this);
            var id = finddiv.find("input:eq(0)").val();
            var item_ids_arr = item_ids.split(',');
            var flag = false;
            for (var i = 0; i < item_ids_arr.length; i++)
            {
                if (item_ids_arr[i] == id) { flag = true;}
            }
            if (flag) {
                finddiv.show();
            }
            else { finddiv.hide(); }
        });
        setTimeout(function(){
            //背景高度调节
            var height=$('#data_table').height();
            if(height>450){
                $('.data_section').css({"background-image":"url('../Public/Home/index/images/Tablelayerbg2.png')","height":"700px"});
            }
            if(height>600)
            {
                $('.data_section').css({"background-image":"url('../Public/Home/index/images/Tablelayerbg3.png')","height":"1320px"});
            }
        },300);

    },

    check_permit:function(){
     //检测时间是否允许
     var url='index.php?m=Detail&a=check_permit';
        var item_id = $('#hiddeninput').val();
        loading();
        $.get(url,{itemid:item_id},function(data){
            done();
           if(data.flag){
               vote.submit();
           }
            else{
               alert(data.msg);
           }
        });
    },
    submit: function () {
        //获取评价项值
        var score1=  $("input[name='num1']:checked").val();
        var score2 = $("input[name='num2']:checked").val();
        var score3 = $("input[name='num3']:checked").val();
        var score4 = $("input[name='num4']:checked").val();
        var score5 = $("input[name='num5']:checked").val();
        var score6 = $("input[name='num6']:checked").val();
        var score7 = $("input[name='num7']:checked").val();
        var score8 = $("input[name='num8']:checked").val();
        var score9 = $("input[name='num9']:checked").val();
        var score10 =$("input[name='num10']:checked").val();
        if (score1 == undefined) score1 = 0;
        if (score2 == undefined) score2 = 0;
        if (score3 == undefined) score3 = 0;
        if (score4 == undefined) score4 = 0;
        if (score5 == undefined) score5 = 0;
        if (score6 == undefined) score6 = 0;
        if (score7 == undefined) score7 = 0;
        if (score8 == undefined) score8 = 0;
        if (score9 == undefined) score9 = 0;
        if (score10==undefined)  score10 = 0;

        var score_arr = [score1, score2, score3, score4, score5, score6, score7,score8,score9,score10];
         var select_one=false;
         for(var i=0;i<score_arr.length;i++){
             if(score_arr[i]>0){
                 select_one=true;break;
             }
         }
        if(!select_one){
            return alert('请至少选择一项你熟悉的评价项进行评价！');
        }
        var score=score_arr.join('-');
        $('#thescore').val(score);

        var item_id = $('#hiddeninput').val();
        var permitcount = parseInt($('#limitcount').val());
        cookie_str = "mycookie";
        if ($.cookie(cookie_str)) {
            var count = 0;
            var temp = $.cookie(cookie_str);
            var array = temp.split('|');
            for (var i = 0; i < array.length; i++)
                if (array[i] == item_id) { count++;}
            if(count>permitcount-1){ return alert("您评价的次数已经超过限制次数，每项最多投"+permitcount+'次票');}
        }
        //先ajax后台看看是否满足条件

        //记录至cookie
        var temp;
        if ($.cookie(cookie_str))
        {
            temp = $.cookie(cookie_str);
            temp += "|";
            temp += item_id;
            $.cookie(cookie_str, temp, { path: '/', expires: 30 });
        }
        else {
            $.cookie(cookie_str, item_id, { path: '/', expires: 30 });
        }
               loading();//检查完后直接提交
        $("#form1").submit();
    }
}

